package com.yicong.ants.ui.coin;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.coin.DelayTransferRecord;
import com.yicong.ants.databinding.RecylerBinding;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.L;

import java.util.HashMap;
import java.util.Map;


/**
 * 延迟转赠记录
 */
public class DelayTransferListFragment extends SimpleLazyFragment<RecylerBinding> {
    StatefulBindQuickAdapter<DelayTransferRecord> mAdapter;
    String target;

    @Override
    protected int getLayoutId() {
        return R.layout.recyler;
    }

    @Override
    public void onFirstUserVisible() {
        target = getArguments().getString(Constants.Extra.Category, "1");
        initAdapter();
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        RecyclerView recyclerView = mDataBind.recycler;
        recyclerView.setBackgroundColor(UiHelper.getColor(R.color.eee_color));
        mAdapter = new StatefulBindQuickAdapter<DelayTransferRecord>(R.layout.delay_transfer_item) {

            @Override
            protected void convert(DataBindViewHolder helper, DelayTransferRecord item) {
                L.d("DelayGiftListFragment", item.toString());
//                helper.getBinding().setVariable(BR.bean, item);
                UiHelper.setVisibleElseGone(helper.getView(R.id.ll_container)
                    , target.equalsIgnoreCase("0") && item.getStatus() == 10);
                helper.setText(R.id.tv_account, item.getTargetText(target));
                helper.getBinding().setVariable(BR.bean, item);

                int status = item.getStatus();
                helper.setTextColor(R.id.tv_starus, UiHelper.getColor(R.color.themeTextOrangeDark));

                //0=普通转赠已到账，10=延迟转赠中，20=延迟转赠立即到账，30=延迟转赠申述中，40=申诉已退还转出方，50=申述已转入收款方
                if (status == 0 || status == 20
                    || status == 40
                    || status == 50) {
                    helper.setTextColor(R.id.tv_starus, UiHelper.getColor(R.color.green_primary));
                }

                helper.getBinding().setVariable(BR.click, (View.OnClickListener) v -> {
                    if (R.id.tv_gift == v.getId()) {
                        transferRecordAction(item.getId(), "1", "确认立即转赠给好友？");
                    } else if (R.id.tv_freeze == v.getId()) {
                        transferRecordAction(item.getId(), "0", "确定冻结转赠，等待客服处理？");
                    }
                });
            }

            @Override
            public void loadPageData(int page) {
//                setNewData(initTestData());
                Map<String, String> params = new HashMap<>();
                params.put("delay", "1");
                params.put("target", target);//0=from_uid 自己转给别人；1=to_uid 别人转给自己
                params.put("page", String.valueOf(page));
                params.put("page_size", Constants.PAGE_SIZE_Str);
                addSubscribe(RetrofitHelper.getApis().transferRecord(params)
                    .compose(RxHelper.toMain())
                    .subscribe(respBean -> {
                        if (respBean.isCodeFail()) {
                            showToast(respBean.getMsg());
                            if (page == 1) {
                                setViewState(MultiStateView.VIEW_STATE_ERROR);
                            }
                            return;
                        }
                        solveData(respBean.getData(), page, Constants.PAGE_SIZE);
                    }, RxHelper.getSwitchableErrorConsumer(this)));
            }
        };

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.loadPageData(1);
    }

    /**
     * @param type 0=冻结；1=立即转赠
     * @param tips "tips"
     */
    void transferRecordAction(String id, String type, String tips) {
        UiHelper.showConfirmActionDialog(mContext, tips, (dialog, which) -> {
            Map<String, String> params = new HashMap<>();
            params.put("id", id);
            params.put("type", type);
            addSubscribe(RetrofitHelper.getApis().transferRecordAction(params)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    if (respBean.isCodeFail()) {
                        showToast(respBean.getMsg());
                        return;
                    }
                    mAdapter.loadPageData(1);
                }, RxHelper.getErrorConsumer()));
        });
    }

    @Override
    protected void onLoadData() {

    }
}
